/**
*      author: Y_Yao
*      created: 2025-04-02 10:33:41
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 7;
int a[2 * N];
int n;
vector<int> p[N];

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++) p[i].clear();
    for(int i = 1;i <= 2*n;i++)
    {
        cin >> a[i];
        p[a[i]].push_back(i);
    }
    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        int p1 = p[i][0],p2 = p[i][1];
        if(p1 == p2 - 1) continue;
        if(p1 > 1)
        {
            if(a[p1-1] == a[p2-1])ans++;
            if(p2 < n*2 && a[p1-1] == a[p2+1]) ans++;
        }
        if(p2 < 2 * n && a[p1+1] == a[p2+1]) ans++;
        if(p1 + 1 != p2 - 1 && p1 + 2 != p2-1 && a[p1+1] == a[p2-1]) ans++;
    }
    cout << ans / 2 << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}